"""
Case Type   : 功能测试
Case Name   : 位串作为octet_length函数的入参
Create At   : 2020/8/2
Owner       : @li-xin12345
Description : position(substring in string)返回特定子字符串的位置。
    步骤 1.查看数据库状态，如果数据库没有启动则执行启动，如果已经启动则无操作
    步骤 2.清理环境删除表防止新建失败
    步骤 3.校验位串的结构
Expect      :
    步骤 1. 成功
    步骤 2. 成功
    步骤 3. 成功
History     :
"""

import os
import unittest
import sys

sys.path.append(sys.path[0] + "/../")
from testcase.utils.Logger import Logger
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH

logger = Logger()

class Bit_string_function(unittest.TestCase):

    def setUp(self):
        logger.info(f'-----{os.path.basename(__file__)} start-----')
        logger.info("-----------查询数据库状态-----------")
        self.commonsh =  CommonSH('dbuser')
        self.common = Common()

    def test_bit_string_in_octet_length(self):
        logger.info("-----------B可以省略-----------")
        sql_cmd = "select octet_length('1111001111101111100'::bit);"
        Normal_SqlMdg = self.commonsh.execut_db_sql(sql_cmd)
        self.assertTrue(int(Normal_SqlMdg.splitlines()[2]) == 1)
        sql_cmd = "select octet_length('1111001111101111100');"
        Normal_SqlMdg = self.commonsh.execut_db_sql(sql_cmd)
        self.assertTrue(int(Normal_SqlMdg.splitlines()[2]) == 19)

        logger.info("-----------全0、全1-----------")
        sql_cmd = "select octet_length(B'00000000000000000000000000');"
        Normal_SqlMdg1 = self.commonsh.execut_db_sql(sql_cmd)
        self.assertTrue(int(Normal_SqlMdg1.splitlines()[2]) == 4)
        sql_cmd = '''SELECT octet_length(B'11111111111111111111111111111');'''
        Normal_SqlMdg1 = self.commonsh.execut_db_sql(sql_cmd)
        self.assertTrue(int(Normal_SqlMdg1.splitlines()[2]) == 4)

        logger.info("-----------数字-----------")
        sql_cmd = '''SELECT octet_length(44::bit(83886080));'''
        Normal_SqlMdg21 = self.commonsh.execut_db_sql(sql_cmd)
        self.assertTrue(int(Normal_SqlMdg21.splitlines()[2]) == 10485760)
        sql_cmd = '''SELECT octet_length(144::bit(33));'''
        Normal_SqlMdg21 = self.commonsh.execut_db_sql(sql_cmd)
        self.assertTrue(int(Normal_SqlMdg21.splitlines()[2]) == 5)

        logger.info("-----------varying(n)-----------")
        sql_cmd = "select octet_length(" \
                  "'1111001111101111111111111111100'::bit varying(1));"
        Normal_SqlMdg321 = self.commonsh.execut_db_sql(sql_cmd)
        self.assertTrue(int(Normal_SqlMdg321.splitlines()[2]) == 1)
        sql_cmd = "select octet_length(" \
                  "'11110011111011111111100'::bit varying(37));"
        Normal_SqlMdg321 = self.commonsh.execut_db_sql(sql_cmd)
        self.assertTrue(int(Normal_SqlMdg321.splitlines()[2]) == 3)
        sql_cmd = "select octet_length(" \
                  "'11110011111011111111100'::bit varying);"
        Normal_SqlMdg321 = self.commonsh.execut_db_sql(sql_cmd)
        self.assertTrue(int(Normal_SqlMdg321.splitlines()[2]) == 3)

        logger.info("-----------bit(n)-----------")
        sql_cmd = "select octet_length(" \
                  "'1111001111101111111111111111100'::bit(1));"
        Normal_SqlMdg321 = self.commonsh.execut_db_sql(sql_cmd)
        self.assertTrue(int(Normal_SqlMdg321.splitlines()[2]) == 1)
        sql_cmd = "select octet_length(" \
                  "'11110011111011111111100'::bit(37));"
        Normal_SqlMdg321 = self.commonsh.execut_db_sql(sql_cmd)
        self.assertTrue(int(Normal_SqlMdg321.splitlines()[2]) == 5)
        sql_cmd = "select octet_length(" \
                  "'11110011111011111111100'::bit);"
        Normal_SqlMdg321 = self.commonsh.execut_db_sql(sql_cmd)
        self.assertTrue(int(Normal_SqlMdg321.splitlines()[2]) == 1)

    def tearDown(self):
        logger.info(f'-----{os.path.basename(__file__)} end-----')